package edu.hunau.recommend.config;


import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



/**
 * @author name
 * @creater 2022-09-23-0:09
 */
@Configuration
public class ElasticsearchClientConfig {

    @Value("${elasticsearch.host}")
    private String hosts;

    @Bean(name = "esClient")
    public ElasticsearchClient esClient(){

        String[] datas = hosts.split(":");

        RestClient restClient = RestClient.builder(
                new HttpHost(datas[0],Integer.parseInt(datas[1]))).build();

        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        ElasticsearchClient client = new ElasticsearchClient(transport);
        return client;
    }
}
